package switchClause

import (
	"fmt"
)

func TestSwitch() {
	score := 85
	/*
		>=90 A
		>=80 B
		>=70 C
		>=60 D
		else E
	*/
	grade := "A"
	if score >= 90 {
		grade = "A"
	} else if score >= 80 {
		grade = "B"
	} else if score >= 70 {
		grade = "C"
	} else if score >= 60 {
		grade = "D"
	} else {
		grade = "E"
	}
	fmt.Println("(if clause)grade: ", grade)

	//其他静态语言，判断单个值
	switch score {
	case 90:
		grade = "A"
	case 80:
		grade = "B"
	case 70:
		grade = "C"
	case 60:
		grade = "D"
	default:
		grade = "E"
	}

	//go语言对switch做了拓展,支持区间判断
	switch {
	case score >= 90:
		grade = "A"
	case score >= 80:
		grade = "B"
	case score >= 70:
		grade = "C"
	case score >= 60:
		grade = "D"
	default:
		grade = "E"
	}

	fmt.Println("(switch clause)grade: ", grade)

	//switch的case支持多条件判断，使用逗号分隔
	//即，或的条件（一分支多值）
	var a = "mum"
	switch a {
	case "mum", "daddy":
		fmt.Println("family")
	}

	//switch做类型判断很方便
	var x interface{}

	switch i := x.(type) {
	case nil:
		fmt.Printf(" x 的类型 :%T", i)
	case int:
		fmt.Printf("x 是 int 型")
	case float64:
		fmt.Printf("x 是 float64 型")
	case func(int) float64:
		fmt.Printf("x 是 func(int) 型")
	case bool, string:
		fmt.Printf("x 是 bool 或 string 型")
	default:
		fmt.Printf("未知型")
	}
}
